/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.model.Classes;
import vn.webapp.model.Rooms;

@Repository("roomsDAO")
@SuppressWarnings({"unchecked", "rawtypes"})
@Transactional
public class RoomsDAOImpl extends BaseDao implements RoomsDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<Rooms> list() {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Rooms.class);
            //criteria.setMaxResults(10);
            //criteria.setFirstResult(0);
            List<Rooms> rooms = criteria.list();
            commit();
            return rooms;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public Rooms viewDetail(String id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM Rooms c WHERE c.room_id = '" + id + "'");
        return (Rooms) q.uniqueResult();
    }
    
    @Override
    public Rooms checkExistsName(String name, int id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM Rooms c WHERE c.name = '" + name + "' AND c.room_id != '" + id + "'");
        return (Rooms) q.uniqueResult();
    }
    
    @Override
    public int updateTheRoom(String name, int id, int capacity){
        Query query = sessionFactory.getCurrentSession().createQuery(
                "UPDATE Rooms SET name ='" + name + "', capacity = '" + capacity + "' WHERE room_id = '" + id + "'");
        int result = query.executeUpdate();
        return result;
    }
    
    @Override
    public int removeRoom(int id) {
        Query query = sessionFactory.getCurrentSession().createQuery(
                "DELETE from Rooms r WHERE r.room_id ='" + id + "'");
        int result = query.executeUpdate();
        return result;
    }
    
    @Override
    public int saveARoom(Rooms room)
    {
        try {
           begin();
           int id = 0; 
           id = (int)getSession().save(room);
           commit();
           return id;           
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<Rooms> filter(int page, int pageSize) {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Rooms.class);
            criteria.setMaxResults(pageSize);
            if (page > 0) {
                criteria.setFirstResult(page * pageSize);
            } else {
                criteria.setFirstResult(page);
            }
            List<Rooms> rooms = criteria.list();
            commit();
            return rooms;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public int count(){
        //int size = 0;
         try {
            begin();
            Criteria criteria = getSession().createCriteria(Rooms.class);            
            List<Rooms> rooms = criteria.list();
            commit();
            return rooms.size();
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
}
